package com.mobileClientKernel.android.serviceManager.featureset;

import android.content.Context;


import com.mobileClientKernel.android.serviceManager.network.IJJResponseCallBack;
import com.mobileClientKernel.android.serviceManager.network.JJBaseRequest;
import com.mobileClientKernel.android.serviceManager.network.JJRequest;
import com.mobileClientKernel.android.serviceManager.network.JJRequestError;
import com.mobileClientKernel.android.serviceManager.service.JJService;

/**
 * Created by JJ on 4/13/16.
 */
public class JJFeatureSet {

    private JJService service;

    public JJFeatureSet(JJService service) {
        this.service = service;
    }

    public void startRequest(final Context context, JJRequest request)
    {
        startRequest(context, request, null);
    }

    public void startRequest(final Context context, final JJRequest request, final IJJAction action)
    {
        request.setContext(context);

        service.recordRequestFinishCount(-1);

        request.startWithCallBack(new IJJResponseCallBack() {
            @Override
            public void onSuccess(JJBaseRequest baseRequest) {
                Object object = request.currentResponseModel();

                if (action != null) {
                    action.onSuccess(object, request);
                }

                service.callBack(context, true, object, null, request);
            }

            @Override
            public void onFailure(JJBaseRequest baseRequest) {
                JJRequestError error = request.getError();

                if (action != null) {
                    action.onFailure(error, request);
                }

                service.callBack(context, false, null, error, request);
            }
        });
    }

    public JJService getService() {
        return service;
    }
    public void setService(JJService service) {
        this.service = service;
    }
}
